
do "E:/ReplicateBuild/02_code/00_environment/00_set_environment.do"

** 2016 data

global sy = 2016

global year_minus_1 = $sy-1

foreach type in "DISAD" "race" "lAch" "pDISAD" "DISADsch" "DISADschwgt" {
	
	
global type = "`type'"
	
if "$type"=="DISAD" {
	global hh = ""
	global mvar = "DISAD"
	global wvar1 = "n_ct_m1_ma"
	global wvar2 = "n_ct_m2_ma"
}
if "$type"=="race" {
	global hh = "_white"
	global mvar = "white"
	global wvar1 = "n_ct_m1_ma"
	global wvar2 = "n_ct_m2_ma"
}
if "$type"=="lAch" {
	global hh = "_achHi_ma"
	global mvar = "achHi_ma"
	global wvar1 = "n_ct_m1_ma"
	global wvar2 = "n_ct_m2_ma"
}
if "$type"=="pDISAD" {
	global hh = ""
	global mvar = "DISAD"
	global wvar1 = "n_ct_m1_ma"
	global wvar2 = "n_ct_m2_ma"
}
if "$type"=="DISADsch" {
	global hh = ""
	global mvar = "DISAD"
	global wvar1 = "n_ct_m1_ma"
	global wvar2 = "n_ct_m2_ma"
}
if "$type"=="DISADschwgt" {
	global hh = ""
	global mvar = "DISAD"
	global wvar1 = "mean_n1"
	global wvar2 = "mean_n2"
}



if "$type"=="pDISAD" {
	use "$temp/student_level_ma_w_unshrunken_VA", clear
	keep DISAD mastid year
	collapse (count) nobs=DISAD (sum) nDISAD=DISAD, by(mastid)

	gen alwaysDISAD = nDISAD==nobs
	drop nobs nDISAD
	tempfile tempmastid
	save `tempmastid', replace
	
	use "$temp/student_level_ma_w_unshrunken_VA", clear
	sort mastid
	merge n:1 mastid using `tempmastid'
	assert _m==3
	drop _m
	
	keep teachid_ma year alwaysDISAD
	
	gen j = teachid_ma
	gen ncerdc_id = j
	gen sy = year
	
	gen notalwaysDISAD = 1-alwaysDISAD
	
	collapse (sum) alwaysDISAD notalwaysDISAD, by(ncerdc_id j sy)
	
	ren notalwaysDISAD n_ct_m1_ma
	ren alwaysDISAD n_ct_m2_ma
	gen p_m1_ma = n_ct_m1_ma / (n_ct_m1_ma+n_ct_m2_ma)
	gen p_m2_ma = n_ct_m2_ma / (n_ct_m1_ma+n_ct_m2_ma)
	
	keep if sy == $sy
	drop sy ncerdc_id
	
	tempfile temppDISAD
	save `temppDISAD', replace
	
}

use "$basedata/mb_clean.dta", clear





use "$basedata/FOCAL_school_year_data", clear
keep if sy==$sy
collapse (max) titleI titleI_eligible, by(ncerdc_lea ncerdc_schlcode)
tempfile tempTitleI
save `tempTitleI', replace

use "$basedata/va_estimates_drift_behavioral", clear

keep j t mu_jt_m1_hat_preY_ma mu_jt_m2_hat_preY_ma
duplicates drop
sort j t
ren mu_jt_m1_hat_preY_ma mu_jt_m1_hat_preY_beh
ren mu_jt_m2_hat_preY_ma mu_jt_m2_hat_preY_beh
tempfile tempbeh
save `tempbeh', replace

use "$basedata/va_estimates_drift${hh}", clear

keep if t==$sy & s!=. & lea_ma=="XXX" // hide identity

if "$type"=="pDISAD" {
	drop p_m1_ma p_m2_ma n_ct_m1_ma n_ct_m2_ma
	

merge n:1 j using `temppDISAD'
drop if _m==2
drop _m
}


merge 1:1 j t using `tempbeh'
drop if _m==2
gen missing_beh = (_m==1)
replace mu_jt_m1_hat_preY_beh = 0 if missing_beh
replace mu_jt_m2_hat_preY_beh = 0 if missing_beh
drop _m

ren j ncerdc_id
ren t sy


sort ncerdc_id sy
merge 1:n ncerdc_id sy using "$basedata/allLEA_teacher_year_data", keepusing(grade*)
assert _m!=1
keep if _m==3
drop _m

ren (ncerdc_id sy) (j t)

ren (lea_ma schlcode_ma) (ncerdc_lea ncerdc_schlcode)
destring ncerdc_lea ncerdc_schlcode, replace

sort ncerdc_lea ncerdc_schlcode
merge n:1 ncerdc_lea ncerdc_schlcode using `tempTitleI'
drop if _m==2
drop _m

keep if mu_jt_m1_hat_preY_ma!=. & mu_jt_m2_hat_preY_ma!=. & p_m1_ma!=. & p_m2_ma!=. & e_ma!=.


keep if inlist(grade_low,"KG","PK") & inlist(grade_high,"04","05")


assert p_m1_ma+p_m2_ma==1

gen n_ct_ma = n_ct_m1_ma + n_ct_m2_ma

bys s: egen mean_n1 = mean(n_ct_m1_ma)
bys s: egen mean_n2 = mean(n_ct_m2_ma)
keep if mu_jt_m1_hat_preY_ma!=. & mu_jt_m2_hat_preY_ma!=. & n_ct_m1_ma!=. & n_ct_m2_ma!=. & e_ma!=. & s!=.

sort j


summ mu_jt_m1_hat_preY_beh [aw=$wvar1]
global total_output_actual_m1 = r(mean)
summ mu_jt_m2_hat_preY_beh [aw=$wvar2]
global total_output_actual_m2 = r(mean)

summ mu_jt_m1_hat_preY_beh
global total_output_random_m1 = r(mean)
summ mu_jt_m2_hat_preY_beh
global total_output_random_m2 = r(mean)

if "$type" == "DISADsch" {
bys ncerdc_lea ncerdc_schlcode: egen s_n_ct_m1_ma = sum(n_ct_m1_ma)
bys ncerdc_lea ncerdc_schlcode: egen s_n_ct_m2_ma = sum(n_ct_m2_ma)

gen schoolDISAD = s_n_ct_m2_ma / (s_n_ct_m1_ma+s_n_ct_m2_ma)

gen DISADcat1 = 1+(schoolDISAD>.3)+(schoolDISAD>.7)
gen DISADcat2 = 1+(schoolDISAD>.7)

summ mu_jt_m1_hat_preY_beh if DISADcat2==1
global total_output_actual_m1 = r(mean)
summ mu_jt_m2_hat_preY_beh if DISADcat2==2
global total_output_actual_m2 = r(mean)

	
}

use "$basedata/mb_clean.dta", clear



* keep only the standard tests
replace score_ma = . if !inlist(ma_test_id,"RG","MA03","MA04","MA05","MA06","MA07","MA08")
* keep only FOCAL
gen FOCAL = lea=="XXX" // hide identity
gen ncerdc_schlcode=real( schlcode) 

	xtset mastid year

	* GEN lagged scores, lagged squares, and lagged cubes
	* CREATE INTERACTIONS OF PRIOR TEST SCORES WITH GRADE LEVEL
	foreach i in ma rd {
		cap gen l_`i'=l.score_`i'
		gen growth_`i' = score_`i'-l_`i'
	}
	foreach ss in ma rd {
	gen achHi_`ss'=(l_`ss'>0)
	}


if "$type"=="pDISAD" {
	sort mastid
	merge n:1 mastid using `tempmastid'
	drop if _m==2
	drop _m
	replace DISAD = alwaysDISAD
}

keep if year==$sy

gen male = 1-female

	gen white=(ethnic==5)
	gen black=(ethnic==4)
	gen hispanic=(ethnic==3)
	gen other_ethnic = 1-white-black-hispanic

keep score_ma score_rd growth_ma growth_rd FOCAL lea schlcode grade white black hispanic other_ethnic male DISAD achHi_ma

keep if grade<=5

* sample sizes
qui count if ${mvar}==0 & FOCAL==0
local sample00 = r(N)
qui count if ${mvar}==0 & FOCAL==1
local sample01 = r(N)
qui count if ${mvar}==1 & FOCAL==0
local sample10 = r(N)
qui count if ${mvar}==1 & FOCAL==1
local sample11 = r(N)

bys lea schlcode: egen schoolDISAD = mean($mvar)


gen DISADcat1 = 1+(schoolDISAD>.3)+(schoolDISAD>.7)
gen DISADcat2 = 1+(schoolDISAD>.7)

local scalevars = "male white black hispanic other_ethnic"

foreach var in `scalevars' {
	replace `var' = `var'*100
}

local testvars = "male white black hispanic other_ethnic score_ma score_rd growth_ma growth_rd"


foreach var in `testvars' {
	qui summ `var' if ${mvar}==0 & FOCAL==1
	local meanS_`var'_21 = r(mean)
	qui summ `var' if ${mvar}>=1 & FOCAL==1
	local meanS_`var'_31 = r(mean)

	qui summ `var' if ${mvar}==0 & FOCAL==0
	local meanS_`var'_20 = r(mean)
	qui summ `var' if ${mvar}>=1 & FOCAL==0
	local meanS_`var'_30 = r(mean)
}

if "$type" == "DISADsch" {

forv d=0/1 {
foreach var in `testvars' {
	ttest `var' if FOCAL==`d', by(DISADcat2)
	local meanS_`var'_2`d' = r(mu_1)
	local meanS_`var'_3`d' = r(mu_2)
}
}
}

mat Sa1 = (.,.,.,.)
mat Sb1 = (`meanS_male_21', `meanS_male_31', `meanS_male_20', `meanS_male_30')
mat Sc1 = (`meanS_white_21', `meanS_white_31', `meanS_white_20', `meanS_white_30')
mat Sd1 = (`meanS_black_21', `meanS_black_31', `meanS_black_20', `meanS_black_30')
mat Se1 = (`meanS_hispanic_21', `meanS_hispanic_31', `meanS_hispanic_20', `meanS_hispanic_30')
mat Sf1 = (`meanS_other_ethnic_21', `meanS_other_ethnic_31', `meanS_other_ethnic_20', `meanS_other_ethnic_30')
mat Sg1 = (.,.,.,.)
mat Sh1 = (`meanS_score_ma_21', `meanS_score_ma_31', `meanS_score_ma_20', `meanS_score_ma_30')
mat Si1 = (`meanS_score_rd_21', `meanS_score_rd_31', `meanS_score_rd_20', `meanS_score_rd_30')
mat Sj1 = (.,.,.,.)
mat Sk1 = (`meanS_growth_ma_21', `meanS_growth_ma_31', `meanS_growth_ma_20', `meanS_growth_ma_30')
mat Sl1 = (`meanS_growth_rd_21', `meanS_growth_rd_31', `meanS_growth_rd_20', `meanS_growth_rd_30')



* build teacher characteristics by school

* teacher characteristics: experience (0, 1-2, 3-5, 6-12, 13-20, 21-27, 28+), Grad degree, regular license, NBPTS certification, Praxis score

use "$basedata/ncerdc_teacher_tests", clear
collapse (mean) teacher_test_score_std, by(ncerdc_id)
ren teacher_test_score_std praxis
tempfile temptest
save `temptest', replace

use "$basedata/ncerdc_education", clear
gen grad_degree = (educational_attainment>=5)
collapse (max) grad_degree, by(ncerdc_id)
tempfile tempeducation
save `tempeducation', replace


use "$basedata/ncerdc_teacher_certification", clear
gen certified = (firstyr<=$sy)
keep ncerdc_id certified
tempfile tempcert
save `tempcert', replace

use "$basedata/ncerdc_teacher_license", clear
collapse (min) firstyr=hq_fy, by(ncerdc_id)
gen licensed = (firstyr<=$sy)
keep ncerdc_id licensed
tempfile templic
save `templic', replace

use "$basedata/allLEA_teacher_year_data", clear
bys ncerdc_id ncerdc_lea ncerdc_schlcode: egen lastyr_school = max(sy)
bys ncerdc_id ncerdc_lea: egen lastyr_district = max(sy)

qui summ sy
local maxyear = r(max)

gen attrition_school = (sy==lastyr_school & sy!=. & sy<`maxyear')
gen attrition_district = (sy==lastyr_district & sy!=. & sy<`maxyear')

keep if sy==$sy

collapse (max) attrition_school attrition_district, by(ncerdc_id)

tempfile tempattrition
save `tempattrition', replace

use "$basedata/ncerdc_demographics", clear
keep ncerdc_id dob
sort ncerdc_id
tempfile tempdob
save `tempdob', replace

use "$basedata/va_estimates_drift${hh}", clear

keep if t==$sy

keep if s!=.
sort j t schlcode lea

bys j t: egen num_obs = count(s)
tab num_obs
drop if num_obs>1
drop num_obs

if "$type"=="pDISAD" {
	drop p_m1_ma p_m2_ma n_ct_m1_ma n_ct_m2_ma
	

merge n:1 j using `temppDISAD'
drop if _m==2
drop _m
}

ren j ncerdc_id
sort ncerdc_id

merge 1:1 ncerdc_id using `temptest'
drop if _m==2
drop _m

merge 1:1 ncerdc_id using `tempeducation'
drop if _m==2
drop _m

merge 1:1 ncerdc_id using `tempcert'
drop if _m==2
drop _m
replace certified = 0 if certified==.

merge 1:1 ncerdc_id using `templic'
drop if _m==2
drop _m
replace licensed = 0 if licensed==.

merge 1:1 ncerdc_id using `tempattrition'
drop if _m==2
drop _m

merge 1:1 ncerdc_id using `tempdob'
drop if _m==2
drop _m
gen age = ${sy}-year(dob)
replace age = . if age<20 | age>80


* experience: e_ma

gen e0 = (e_ma==0)
gen e1_2 = (e_ma>=1 & e_ma<=2)
gen e3_5 = (e_ma>=3 & e_ma<=5)
gen e6_12 = (e_ma>=6 & e_ma<=12)
gen e13_20 = (e_ma>=13 & e_ma<=20)
gen e21_27 = (e_ma>=21 & e_ma<=27)
gen e28 = (e_ma>=28)

gen FOCAL = lea_ma=="XXX" // hide identity

bys lea_ma schlcode_ma: egen s_n_ct_m1_ma = sum(n_ct_m1_ma)
bys lea_ma schlcode_ma: egen s_n_ct_m2_ma = sum(n_ct_m2_ma)

gen schoolDISAD = s_n_ct_m2_ma / (s_n_ct_m1_ma+s_n_ct_m2_ma)

gen DISADcat1 = 1+(schoolDISAD>.3)+(schoolDISAD>.7)
gen DISADcat2 = 1+(schoolDISAD>.7)


local scalevars = "e0 e1_2 e3_5 e6_12 e13_20 e21_27 e28 grad_degree licensed certified attrition_school attrition_district"

foreach var in `scalevars' {
	replace `var' = `var'*100
}

local testvars = "`scalevars' praxis age"

bys lea_ma schlcode_ma: egen mean_n1 = mean(n_ct_m1_ma)
bys lea_ma schlcode_ma: egen mean_n2 = mean(n_ct_m2_ma)


foreach var in `testvars' {
	qui summ `var' if FOCAL==1 [aw=$wvar1]
	local meanS_`var'_21 = r(mean)
	qui summ `var' if FOCAL==1 [aw=$wvar2]
	local meanS_`var'_31 = r(mean)

	qui summ `var' if FOCAL==0 [aw=$wvar1]
	local meanS_`var'_20 = r(mean)
	qui summ `var' if FOCAL==0 [aw=$wvar2]
	local meanS_`var'_30 = r(mean)
}

if "$type" == "DISADsch" {

forv d=0/1 {
foreach var in `testvars' {
	ttest `var' if FOCAL==`d', by(DISADcat2)
	local meanS_`var'_2`d' = r(mu_1)
	local meanS_`var'_3`d' = r(mu_2)
}
}
}


mat Sa2 = (.,.,.,.)
mat Sb2 = (.,.,.,.)
mat Sc2 = (`meanS_e0_21', `meanS_e0_31', `meanS_e0_20', `meanS_e0_30')
mat Sd2 = (`meanS_e1_2_21', `meanS_e1_2_31', `meanS_e1_2_20', `meanS_e1_2_30')
mat Se2 = (`meanS_e3_5_21', `meanS_e3_5_31', `meanS_e3_5_20', `meanS_e3_5_30')
mat Sf2 = (`meanS_e6_12_21', `meanS_e6_12_31', `meanS_e6_12_20', `meanS_e6_12_30')
mat Sg2 = (`meanS_e13_20_21', `meanS_e13_20_31', `meanS_e13_20_20', `meanS_e13_20_30')
mat Sh2 = (`meanS_e21_27_21', `meanS_e21_27_31', `meanS_e21_27_20', `meanS_e21_27_30')
mat Si2 = (`meanS_e28_21', `meanS_e28_31', `meanS_e28_20', `meanS_e28_30')
mat Sj2 = (`meanS_grad_degree_21', `meanS_grad_degree_31', `meanS_grad_degree_20', `meanS_grad_degree_30')
mat Sk2 = (`meanS_licensed_21', `meanS_licensed_31', `meanS_licensed_20', `meanS_licensed_30')
mat Sl2 = (`meanS_certified_21', `meanS_certified_31', `meanS_certified_20', `meanS_certified_30')
mat Sm2 = (`meanS_praxis_21', `meanS_praxis_31', `meanS_praxis_20', `meanS_praxis_30')
mat Sm2b = (`meanS_age_21', `meanS_age_31', `meanS_age_20', `meanS_age_30')
mat Sn2 = (.,.,.,.)
mat So2 = (`meanS_attrition_school_21', `meanS_attrition_school_31', `meanS_attrition_school_20', `meanS_attrition_school_30')
mat Sp2 = (`meanS_attrition_district_21', `meanS_attrition_district_31', `meanS_attrition_district_20', `meanS_attrition_district_30')




use "$basedata/va_estimates_drift${hh}", clear

sort j t s

keep if s!=.

forv ee=0/5 {
summ alphaZ_ma if e_ma==`ee'
global alphaZ`ee' = r(mean)
}


if "$type"=="pDISAD" {
	drop p_m1_ma p_m2_ma n_ct_m1_ma n_ct_m2_ma
	

merge n:1 j using `temppDISAD'
drop if _m==2
drop _m
}


ren Abar_ma u_hetero
ren mu_jt_hat_preY_ma s_hetero

ren mu_jt_m1_hat_preY_ma s_hetero_m1
ren mu_jt_m2_hat_preY_ma s_hetero_m2

ren Abar_m1_ma u_hetero_m1
ren Abar_m2_ma u_hetero_m2

replace u_hetero = u_hetero + ${alphaZ5} if e_ma>=5
replace u_hetero_m1 = u_hetero_m1 + ${alphaZ5} if e_ma>=5
replace u_hetero_m2 = u_hetero_m2 + ${alphaZ5} if e_ma>=5 
forv e=0/4 {
replace u_hetero = u_hetero + ${alphaZ`e'} if e_ma==`e'
replace u_hetero_m1 = u_hetero_m1 + ${alphaZ`e'} if e_ma==`e'
replace u_hetero_m2 = u_hetero_m2 + ${alphaZ`e'} if e_ma==`e'	
}


sort j t schlcode lea

bys j t: egen num_obs = count(s)
tab num_obs
drop if num_obs>1
drop num_obs

merge 1:n j t schlcode lea using "$basedata/va_homogeneous_estimates_drift", keepusing(mu_jt_hat_preY_ma Abar_ma)
drop if _m==2
drop _m

bys j t: egen num_obs = count(s)
tab num_obs
drop if num_obs>1
drop num_obs

ren Abar_ma u_homog
ren mu_jt_hat_preY_ma s_homog

merge 1:n j t schlcode lea using "$basedata/va_homogeneous_estimates_drift_classmeans_cfr", keepusing(mu_homog_ma_cfr)
drop if _m==2
drop _m

bys j t: egen num_obs = count(s)
tab num_obs
drop if num_obs>1
drop num_obs


sort j t
merge 1:n j t schlcode lea using "$basedata/va_estimates_drift_schoolMeans", keepusing(mu_jt_hat_preY_ma Abar_ma mu_jt_m1_hat_preY_ma mu_jt_m2_hat_preY_ma)
drop if _m==2
drop _m

bys j t: egen num_obs = count(s)
tab num_obs
drop if num_obs>1
drop num_obs

ren Abar_ma u_sM
ren mu_jt_hat_preY_ma s_sM
ren mu_jt_m1_hat_preY_ma s_sM_m1
ren mu_jt_m2_hat_preY_ma s_sM_m2


sort j t
merge 1:1 j t using "$temp/VAimputedhired", keepusing(VA1_ma_impute VA2_ma_impute VA1_imputed_0samp VA2_imputed_0samp VA1_imputed_shr VA2_imputed_shr)
drop if _m==2
drop _m

sort j t
merge 1:n j t using "$basedata/va_estimates_drift_behavioral", keepusing(mu_jt_hat_preY_ma Abar_ma mu_jt_m1_hat_preY_ma mu_jt_m2_hat_preY_ma)
drop if _m==2
drop _m


bys j t: egen num_obs = count(s)
tab num_obs
drop if num_obs>1
drop num_obs

ren Abar_ma u_beh
ren mu_jt_hat_preY_ma s_beh
ren mu_jt_m1_hat_preY_ma s_beh_m1
ren mu_jt_m2_hat_preY_ma s_beh_m2

gen s_cfr_nonmiss = t if mu_homog_ma_cfr!=.
bys j: egen firstyr = min(s_cfr_nonmiss)


keep if t==$sy

keep if s!=.
sort j t schlcode lea


gen FOCAL = lea_ma=="XXX" // hide identity

bys lea_ma schlcode_ma: egen s_n_ct_m1_ma = sum(n_ct_m1_ma)
bys lea_ma schlcode_ma: egen s_n_ct_m2_ma = sum(n_ct_m2_ma)

bys lea_ma schlcode_ma: egen mean_n1 = mean(n_ct_m1_ma)
bys lea_ma schlcode_ma: egen mean_n2 = mean(n_ct_m2_ma)


gen schoolDISAD = s_n_ct_m2_ma / (s_n_ct_m1_ma+s_n_ct_m2_ma)

replace s_beh = s_beh_m1*(1-schoolDISAD) + s_beh_m2*schoolDISAD
gen s_beh_trim = s_beh if abs(s_beh)<1
gen s_beh_trim_m1 = s_beh_m1 if abs(s_beh)<1
gen s_beh_trim_m2 = s_beh_m2 if abs(s_beh)<1

gen DISADcat1 = 1+(schoolDISAD>.3)+(schoolDISAD>.7)
gen DISADcat2 = 1+(schoolDISAD>.7)

gen s_homog_m1 = s_homog
gen s_homog_m2 = s_homog

gen s_imp0_m1 = s_hetero_m1
replace s_imp0_m1 = VA1_imputed_0samp if s_imp0_m1==.
gen s_imp0_m2 = s_hetero_m2
replace s_imp0_m2 = VA2_imputed_0samp if s_imp0_m2==.
gen s_imp0 = s_imp0_m1*(1-schoolDISAD) + s_imp0_m2*schoolDISAD

gen imputed = s_hetero_m1==. & s_hetero_m2==.
gen imputed_m1 = imputed
gen imputed_m2 = imputed

gen s_cfr = mu_homog_ma_cfr
gen s_cfr_m1 = s_cfr
gen s_cfr_m2 = s_cfr




local testvars = "s_hetero s_homog s_sM s_beh s_beh_trim s_imp0 imputed u_hetero s_cfr"



foreach var in `testvars' {
	qui summ `var' if FOCAL==1 [aw=$wvar1]
	local meanS_`var'_21 = r(mean)
	qui summ `var' if FOCAL==1 [aw=$wvar2]
	local meanS_`var'_31 = r(mean)

	qui summ `var' if FOCAL==0 [aw=$wvar1]
	local meanS_`var'_20 = r(mean)
	qui summ `var' if FOCAL==0 [aw=$wvar2]
	local meanS_`var'_30 = r(mean)
	
	qui summ `var'_m1 if FOCAL==1 [aw=$wvar1]
	local meanS_`var'_m_21 = r(mean)
	qui summ `var'_m2 if FOCAL==1 [aw=$wvar2]
	local meanS_`var'_m_31 = r(mean)

	qui summ `var'_m1 if FOCAL==0 [aw=$wvar1]
	local meanS_`var'_m_20 = r(mean)
	qui summ `var'_m2 if FOCAL==0 [aw=$wvar2]
	local meanS_`var'_m_30 = r(mean)
}

if "$type" == "DISADsch" {

forv d=0/1 {
foreach var in `testvars' {
	ttest `var' if FOCAL==`d', by(DISADcat2)
	local meanS_`var'_2`d' = r(mu_1)
	local meanS_`var'_3`d' = r(mu_2)
	
	ttest `var'_m1 if FOCAL==`d', by(DISADcat2)
	local meanS_`var'_m_2`d' = r(mu_1)

	ttest `var'_m2 if FOCAL==`d', by(DISADcat2)
	local meanS_`var'_m_3`d' = r(mu_2)
}
}
}


foreach var in "s_hetero" "s_imp0" {
qui summ `var'_m1 if FOCAL==1 [aw=$wvar2]
local meanS_`var'_mis_21 = r(mean)
qui summ `var'_m2 if FOCAL==1 [aw=$wvar1]
local meanS_`var'_mis_31 = r(mean)

qui summ `var'_m1 if FOCAL==0 [aw=$wvar2]
local meanS_`var'_mis_20 = r(mean)
qui summ `var'_m2 if FOCAL==0 [aw=$wvar1]
local meanS_`var'_mis_30 = r(mean)
}

if "$type" == "DISADsch" {

forv d=0/1 {
local var = "s_hetero"
	
	ttest `var'_m1 if FOCAL==`d', by(DISADcat2)
	local meanS_`var'_mis_2`d' = r(mu_2)

	ttest `var'_m2 if FOCAL==`d', by(DISADcat2)
	local meanS_`var'_mis_3`d' = r(mu_1)



}
}



mat Sa4 = (.,.,.,.)
mat Sb4a = (`meanS_s_hetero_21', `meanS_s_hetero_mis_31', `meanS_s_hetero_20', `meanS_s_hetero_mis_30')
mat Sb4b = (`meanS_s_hetero_mis_21', `meanS_s_hetero_31', `meanS_s_hetero_mis_20', `meanS_s_hetero_30')
mat Sc4 = (`meanS_s_homog_m_21', `meanS_s_homog_m_31',`meanS_s_homog_m_20', `meanS_s_homog_m_30')
mat Sd4 = (`meanS_s_sM_m_21', `meanS_s_sM_m_31',`meanS_s_sM_m_20', `meanS_s_sM_m_30')
mat Se4a = (`meanS_s_imp0_mis_21', `meanS_s_imp0_31', `meanS_s_imp0_mis_20', `meanS_s_imp0_30')
mat Se4b = (`meanS_s_imp0_21', `meanS_s_imp0_mis_31', `meanS_s_imp0_20', `meanS_s_imp0_mis_30')
mat Se4c = (`meanS_s_cfr_m_21', `meanS_s_cfr_m_31', `meanS_s_cfr_m_20', `meanS_s_cfr_m_30')
mat Se4d = (`meanS_imputed_m_21', `meanS_imputed_m_31', `meanS_imputed_m_20', `meanS_imputed_m_30')
mat Se4e = (`meanS_u_hetero_m_21', `meanS_u_hetero_m_31', `meanS_u_hetero_m_20', `meanS_u_hetero_m_30')
mat Sf4 = (.,.,.,.)
mat Sg4 = (${total_output_actual_m1}-${total_output_random_m1}, ${total_output_actual_m2}-${total_output_random_m2}, .,.)



gen a1var = .
gen b1var = .
gen c1var = .
gen d1var = .
gen e1var = .
gen g1var = .
gen h1var = .
gen i1var = .
gen j1var = .
gen k1var = .
gen l1var = .

label var a1var "\emph{Students}"
label var b1var "Male (\%)"
label var c1var "White (\%)"
label var d1var "Black (\%)"
label var e1var "Hispanic (\%)"
label var g1var "\emph{Student performance (level scores)}"
label var h1var "Math"
label var i1var "Reading"
label var j1var "\emph{Student performance (gain scores)}"
label var k1var "Math"
label var l1var "Reading"



mat rownames Sa1 = a1var
mat rownames Sb1 = b1var
mat rownames Sc1 = c1var
mat rownames Sd1 = d1var
mat rownames Se1 = e1var
mat rownames Sg1 = g1var
mat rownames Sh1 = h1var
mat rownames Si1 = i1var
mat rownames Sj1 = j1var
mat rownames Sk1 = k1var
mat rownames Sl1 = l1var

matrix Smat1short=Sa1\Sc1\Sd1\Se1\Sg1\Sh1\Sj1\Sk1

mat Sghi2 = Sg2+Sh2+Si2


gen a2var = .
gen b2var = .
gen c2var = .
gen d2var = .
gen e2var = .
gen f2var = .
gen j2var = .
gen k2var = .
gen l2var = .
gen m2var = .
gen m2bvar = .

gen ghi2var = .

label var a2var "\emph{Teachers}"
label var b2var "Experience (\% of teachers)"
label var c2var "\hspace{1cm}   0 years"
label var d2var "\hspace{1cm}   1-2 years"
label var e2var "\hspace{1cm}   3-5 years"
label var f2var "\hspace{1cm}   6-12 years"
label var j2var "Graduate degree (\%)"
label var k2var "Regular license (\%)"
label var l2var "NBPTS certified (\%)"
label var m2var "Praxis score"
label var m2bvar "Age"


label var ghi2var "\hspace{1cm}   13 or more years"


mat rownames Sa2 = a2var
mat rownames Sb2 = b2var
mat rownames Sc2 = c2var
mat rownames Sd2 = d2var
mat rownames Se2 = e2var
mat rownames Sf2 = f2var
mat rownames Sj2 = j2var
mat rownames Sk2 = k2var
mat rownames Sl2 = l2var
mat rownames Sm2 = m2var
mat rownames Sm2b = m2bvar

mat rownames Sghi2 = ghi2var

matrix Smat2short=Sa2\Sb2\Sc2\Sd2\Se2\Sf2\Sghi2\Sj2\Sk2\Sl2\Sm2\Sm2b



gen a4var = .
gen b4vara = .
gen b4varb = .
gen c4var = .
gen d4var = .
gen e4vara = .
gen e4varb = .
gen e4varc = .
gen e4vard = .
gen e4vare = .
gen f4var = .
gen g4var = .

label var a4var "\emph{Mean math value-added}"
label var b4vara "Baseline, econ disadv"
label var b4varb "Baseline, econ adv"

if "$type"=="race" {
	label var b4vara "Baseline, white"
	label var b4varb "Baseline, non-white"
}

if "$type"=="lAch" {
	label var b4vara "Baseline, above"
	label var b4varb "Baseline, below"
}


label var c4var "Homogeneous"
label var d4var "Using school means"
label var e4vara "Imputed, econ disadv"
label var e4varb "Imputed, econ adv"
label var e4varc "CFR"
label var e4vard "Fraction imputed"
label var e4vare "Residual"
label var f4var "\emph{Mean behavioral value-added}"
label var g4var "Baseline"


mat rownames Sa4 = a4var
mat rownames Sb4a = b4vara
mat rownames Sb4b = b4varb
mat rownames Sc4 = c4var
mat rownames Sd4 = d4var
mat rownames Se4a = e4vara
mat rownames Se4b = e4varb
mat rownames Se4c = e4varc
mat rownames Se4d = e4vard
mat rownames Se4e = e4vare
mat rownames Sf4 = f4var
mat rownames Sg4 = g4var


mat Sa5 = (.,.,.,.)
mat Sb5 = (`sample01',`sample11',`sample00',`sample10')
gen a5var = .
gen b5var = .
label var a5var "\emph{Sample size}"
label var b5var "Number of students"
mat rownames Sa5 = a5var
mat rownames Sb5 = b5var



matrix Smfullshort = Smat1short\Smat2short\Sa4\Sb4a\Sb4b\Sc4\Se4c\Sd4\Se4a\Se4b\Se4d\Se4e\Sf4\Sg4\Sa5\Sb5

if "$type"=="DISAD" {
frmttable using "$tables/characteristics_by_disdadv_extra", statmat(Smfullshort) replace va tex fra ///
	ctitles("","Focal, Adv","Focal, Disadv","Other, Adv","Other, Disadv") ///
	sdec(0,0,0,0 \ 2,2,2,2 \ 2,2,2,2 \ 2,2,2,2 \ 0,0,0,0 \ 2,2,2,2 \ 0,0,0,0 \ 2,2,2,2 \ 0,0,0,0 \ 0,0,0,0 \ 2,2,2,2 \ 2,2,2,2 \ 2,2,2,2 \ 2,2,2,2 \ 2,2,2,2 \ 2,2,2,2 \ 2,2,2,2 \ 2,2,2,2 \ 2,2,2,2 \ 0,0,0,0 \ 0,0,0,0 \ 2,2,2,2 \ 2,2,2,2 \ 2,2,2,2 \ 2,2,2,2 \ 2,2,2,2 \ 2,2,2,2 \ 2,2,2,2 \ 2,2,2,2 \ 2,2,2,2 \ 0,0,0,0 \ 2,2,0,0 \ 0,0,0,0 \ 0,0,0,0  )
}
if "$type"=="race" {
frmttable using "$tables/characteristics_by_disdadv_extra_race", statmat(Smfullshort) replace va tex fra ///
	ctitles("","Focal, Non-white","Focal, White","Other, Non-white","Other, White") ///
	sdec(0,0,0,0 \ 2,2,2,2 \ 2,2,2,2 \ 2,2,2,2 \ 0,0,0,0 \ 2,2,2,2 \ 0,0,0,0 \ 2,2,2,2 \ 0,0,0,0 \ 0,0,0,0 \ 2,2,2,2 \ 2,2,2,2 \ 2,2,2,2 \ 2,2,2,2 \ 2,2,2,2 \ 2,2,2,2 \ 2,2,2,2 \ 2,2,2,2 \ 2,2,2,2 \ 0,0,0,0 \ 0,0,0,0 \ 2,2,2,2 \ 2,2,2,2 \ 2,2,2,2 \ 2,2,2,2 \ 2,2,2,2 \ 2,2,2,2 \ 2,2,2,2 \ 2,2,2,2 \ 2,2,2,2 \ 0,0,0,0 \ 2,2,0,0 \ 0,0,0,0 \ 0,0,0,0)
}
if "$type"=="lAch" {
frmttable using "$tables/characteristics_by_disdadv_extra_lAch", statmat(Smfullshort) replace va tex fra ///
	ctitles("","Focal, Below","Focal, Above","Other, Below","Other, Above") ///
	sdec(0,0,0,0 \ 2,2,2,2 \ 2,2,2,2 \ 2,2,2,2 \ 0,0,0,0 \ 2,2,2,2 \ 0,0,0,0 \ 2,2,2,2 \ 0,0,0,0 \ 0,0,0,0 \ 2,2,2,2 \ 2,2,2,2 \ 2,2,2,2 \ 2,2,2,2 \ 2,2,2,2 \ 2,2,2,2 \ 2,2,2,2 \ 2,2,2,2 \ 2,2,2,2 \ 0,0,0,0 \ 0,0,0,0 \ 2,2,2,2 \ 2,2,2,2 \ 2,2,2,2 \ 2,2,2,2 \ 2,2,2,2 \ 2,2,2,2 \ 2,2,2,2 \ 2,2,2,2 \ 2,2,2,2 \ 0,0,0,0 \ 2,2,0,0 \ 0,0,0,0 \ 0,0,0,0)
}
if "$type"=="pDISAD" {
frmttable using "$tables/characteristics_by_disdadv_extra_pDISAD", statmat(Smfullshort) replace va tex fra ///
	ctitles("","Focal, Adv","Focal, Disadv","Other, Adv","Other, Disadv") ///
	sdec(0,0,0,0 \ 2,2,2,2 \ 2,2,2,2 \ 2,2,2,2 \ 0,0,0,0 \ 2,2,2,2 \ 0,0,0,0 \ 2,2,2,2 \ 0,0,0,0 \ 0,0,0,0 \ 2,2,2,2 \ 2,2,2,2 \ 2,2,2,2 \ 2,2,2,2 \ 2,2,2,2 \ 2,2,2,2 \ 2,2,2,2 \ 2,2,2,2 \ 2,2,2,2 \ 0,0,0,0 \ 0,0,0,0 \ 2,2,2,2 \ 2,2,2,2 \ 2,2,2,2 \ 2,2,2,2 \ 2,2,2,2 \ 2,2,2,2 \ 2,2,2,2 \ 2,2,2,2 \ 2,2,2,2 \ 0,0,0,0 \ 2,2,0,0 \ 0,0,0,0 \ 0,0,0,0)
}
if "$type"=="DISADsch" {
frmttable using "$tables/characteristics_by_disdadv_extra_school", statmat(Smfullshort) replace va tex fra ///
	ctitles("","Focal, Adv","Focal, Disadv","Other, Adv","Other, Disadv") ///
	sdec(0,0,0,0 \ 2,2,2,2 \ 2,2,2,2 \ 2,2,2,2 \ 0,0,0,0 \ 2,2,2,2 \ 0,0,0,0 \ 2,2,2,2 \ 0,0,0,0 \ 0,0,0,0 \ 2,2,2,2 \ 2,2,2,2 \ 2,2,2,2 \ 2,2,2,2 \ 2,2,2,2 \ 2,2,2,2 \ 2,2,2,2 \ 2,2,2,2 \ 2,2,2,2 \ 0,0,0,0 \ 0,0,0,0 \ 2,2,2,2 \ 2,2,2,2 \ 2,2,2,2 \ 2,2,2,2 \ 2,2,2,2 \ 2,2,2,2 \ 2,2,2,2 \ 2,2,2,2 \ 2,2,2,2 \ 0,0,0,0 \ 2,2,0,0 \ 0,0,0,0 \ 0,0,0,0)
}
if "$type"=="DISADschwgt" {
frmttable using "$tables/characteristics_by_disdadv_extra_schoolwgt", statmat(Smfullshort) replace va tex fra ///
	ctitles("","Focal, Adv","Focal, Disadv","Other, Adv","Other, Disadv") ///
	sdec(0,0,0,0 \ 2,2,2,2 \ 2,2,2,2 \ 2,2,2,2 \ 0,0,0,0 \ 2,2,2,2 \ 0,0,0,0 \ 2,2,2,2 \ 0,0,0,0 \ 0,0,0,0 \ 2,2,2,2 \ 2,2,2,2 \ 2,2,2,2 \ 2,2,2,2 \ 2,2,2,2 \ 2,2,2,2 \ 2,2,2,2 \ 2,2,2,2 \ 2,2,2,2 \ 0,0,0,0 \ 0,0,0,0 \ 2,2,2,2 \ 2,2,2,2 \ 2,2,2,2 \ 2,2,2,2 \ 2,2,2,2 \ 2,2,2,2 \ 2,2,2,2 \ 2,2,2,2 \ 2,2,2,2 \ 0,0,0,0 \ 2,2,0,0 \ 0,0,0,0 \ 0,0,0,0)
}
}